import type { Schema, UISchema } from 'gantd/lib/schema-form';
import type { Moment } from 'moment';
import moment from 'moment';
import { assign, cloneDeep } from 'lodash';
import { fieldDataSource } from './fieldMaps';
/**
 * 字段对应的各种组件
 */
export type MetadataFieldPropsMaps = Record<string, MetadataFieldPropsSchema>;
export interface MetadataFieldPropsSchema extends Schema {
  aliasName?: string;
  defaultValue?: any;
  uiScema?: UISchema;
  reactPropsName?: string;
}
export interface FieldPropsMaps {
  requiredFieldProps: MetadataFieldPropsMaps;
  defaultFieldProps: MetadataFieldPropsMaps;
  metadataFieldPropsMaps: MetadataFieldPropsMaps;
}
const requiredFieldProps: MetadataFieldPropsMaps = {
  title: {
    title: tr('字段名'),
    required: true,
    props: {
      placeholder: tr('请输入字段名'),
    },
  },
  memo: {
    title: tr('字段描述'),
    componentType: 'TextArea',
    props: {
      placeholder: tr('请输入字段描述'),
      autoSize: { minRows: 3, maxRows: 10 },
    },
  },
  sourceEntryTitle: {
    title: tr('源对象'),
    props: {
      placeholder: tr('请输入源对象'),
    },
  },
  propComponent: {
    title: tr('组件类型'),
    componentType: 'Selector',
    required: true,
    props: {
      useStorage: false,
      dataSource: fieldDataSource(),
      placeholder: tr('选择组件类型'),
    },
  },
};

const defaultFieldProps: MetadataFieldPropsMaps = {
  emptyText: {
    title: tr('空白提示'),
    type: 'string',
    reactPropsName: 'placeholder',
    props: {
      placeholder: tr('请输入空白提示'),
    },
  },
};
const metadataFieldPropsMaps: MetadataFieldPropsMaps = {
  defaultValue: {
    title: tr('默认值'),
    reactPropsName: 'initialValue',
    dependencies: ['maxLength', 'regex'],
    props: {
      placeholder: tr('请输入默认值'),
    },
    onDependenciesChange: ([maxLength, regex], schema) => {
      return {
        ...schema,
        props: {
          ...schema.props,
          maxLength,
        },
        options: {
          rules: regex ? [{ pattern: eval(regex), message: tr('默认值格式错误') }] : null,
        },
      };
    },
  },
  numberDefaultValue: {
    title: tr('默认值'),
    reactPropsName: 'initialValue',
    componentType: 'InputNumber',
    dependencies: ['minValue', 'maxValue', 'integerOnly', 'precision'],
    onDependenciesChange: ([minValue, maxValue, integerOnly, precision], schema) => {
      return {
        ...schema,
        props: {
          ...schema.props,
          min: minValue,
          max: maxValue,
          precision: integerOnly ? precision : 0,
        },
      };
    },
    props: {
      placeholder: tr('请输入默认值'),
    },
  },
  dateDefaultValue: {
    title: tr('默认值'),
    aliasName: 'defaultValue',
    componentType: 'DatePicker',
    reactPropsName: 'initialValue',
    dependencies: ['minValue', 'maxValue'],
    props: {
      placeholder: tr('请选择默认值'),
    },
    onDependenciesChange: ([minValue, maxValue], schema) => {
      return {
        ...schema,
        props: {
          ...schema.props,
          disabledDate: (current: Moment) => {
            return (
              (current && minValue && current < moment(minValue).endOf('day')) ||
              (current && maxValue && current > moment(maxValue).startOf('day'))
            );
          },
        },
      };
    },
  },
  rangeDateDefaultValue: {
    title: tr('默认值'),
    aliasName: 'defaultValue',
    componentType: 'RangePicker',
    reactPropsName: 'initialValue',
    props: {
      placeholder: tr('请选择默认值'),
    },
  },
  maxLength: {
    title: tr('最大长度'),
    componentType: 'InputNumber',
    type: 'number',
    defaultValue: 10,
    required: true,
    props: {
      placeholder: tr('允许输入的最大长度'),
      precision: 0,
      min: 5,
    },
  },
  regex: {
    title: tr('文本格式'),
    componentType: 'Selector',
    type: 'string',
    reactPropsName: 'pattern',
    props: {
      useStorage: false,
      dataSource: [
        {
          label: tr('邮箱'),
          value: '/w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*/',
        },
        {
          label: tr('手机号'),
          value: '/^1[3456789]\\d{9}$/',
        },
        {
          label: tr('固定电话'),
          value: '/^((d{3,4})|d{3,4}-|s)?d{7,14}$/',
        },
        {
          label: tr('网址'),
          value: '/^https?://(([a-zA-Z0-9_-])+(.)?)*(:d+)?(/((.)?(?)?=?&?[a-zA-Z0-9_-](?)?)*)*$/i',
        },
        {
          label: tr('身份证号'),
          value:
            '/^(^[1-9]d{7}((0d)|(1[0-2]))(([0|1|2]d)|3[0-1])d{3}$)|(^[1-9]d{5}[1-9]d{3}((0d)|(1[0-2]))(([0|1|2]d)|3[0-1])((d{4})|d{3}[Xx])$)$/',
        },
      ],
    },
  },
  maxValue: {
    title: tr('最大值'),
    componentType: 'InputNumber',
    reactPropsName: 'max',
    dependencies: ['minValue', 'integerOnly', 'precision'],
    onDependenciesChange: ([minValue, integerOnly, precision], schema) => {
      return {
        ...schema,
        props: {
          ...schema.props,
          min: minValue,
          precision: integerOnly ? precision : 0,
        },
      };
    },
    props: {
      placeholder: tr('请输入最大值'),
    },
  },
  minValue: {
    title: tr('最小值'),
    componentType: 'InputNumber',
    dependencies: ['maxValue', 'integerOnly', 'precision'],
    reactPropsName: 'min',
    onDependenciesChange: ([maxValue, integerOnly, precision], schema) => {
      return {
        ...schema,
        props: {
          ...schema.props,
          max: maxValue,
          precision: integerOnly ? precision : 0,
        },
      };
    },
    props: {
      placeholder: tr('请输入最小值'),
    },
  },
  maxDate: {
    title: tr('最大时间'),
    componentType: 'DatePicker',
    aliasName: 'maxValue',
    dependencies: ['minValue'],
    onDependenciesChange: ([minValue], schema) => {
      return {
        ...schema,
        props: {
          ...schema.props,
          disabledDate: (current: Moment) => {
            // Can not select days before today and today
            return current && current < moment(minValue).endOf('day');
          },
        },
      };
    },
    props: {
      placeholder: tr('请选择最大时间'),
    },
  },
  minDate: {
    title: tr('最小时间'),
    componentType: 'DatePicker',
    aliasName: 'minValue',
    props: {
      placeholder: tr('请选择最小时间'),
    },
    dependencies: ['maxValue'],
    onDependenciesChange: ([maxValue], schema) => {
      return {
        ...schema,
        props: {
          ...schema.props,
          disabledDate: (current: Moment) => {
            // Can not select days before today and today
            return current && current > moment(maxValue).endOf('day');
          },
        },
      };
    },
  },
  dateType: {
    title: tr('时间格式'),
    componentType: 'Selector',
    defaultValue: 'date',
    reactPropsName: 'format',
    props: {
      useStorage: false,
      dataSource: [
        {
          label: '日期',
          value: 'date',
        },
        {
          label: '日期时间',
          value: 'dateType',
        },
      ],
    },
  },
  multipleOptionsDefalutValue: {
    title: tr('默认值'),
    aliasName: 'defaultValue',
    componentType: 'Selector',
    reactPropsName: 'initialValue',
    dependencies: ['options'],
    onDependenciesChange: ([options], schema) => {
      return {
        ...schema,
        props: {
          ...schema.props,
          dataSource: options,
        },
      };
    },
    props: {
      dataSource: [],
      mode: 'multiple',
      useStorage: false,
      placeholder: tr('请选择默认值'),
    },
  },
  optionsDefalutValue: {
    title: tr('默认值'),
    aliasName: 'defaultValue',
    reactPropsName: 'initialValue',
    componentType: 'Selector',
    dependencies: ['options'],
    onDependenciesChange: ([options], schema) => {
      return {
        ...schema,
        props: {
          ...schema.props,
          dataSource: options,
        },
      };
    },
    props: {
      dataSource: [],
      useStorage: false,
      placeholder: tr('请选择默认值'),
    },
  },
  options: {
    componentType: 'SelectEdit',
    reactPropsName: 'dataSource',
    props: {
      title: `${tr('选项')}:`,
    },
    defaultValue: [
      {
        label: `${tr('选项')}1`,
        value: `${tr('选项')}1`,
      },
      {
        label: `${tr('选项')}2`,
        value: `${tr('选项')}2`,
      },
      {
        label: `${tr('选项')}3`,
        value: `${tr('选项')}3`,
      },
    ],
  },
  multiple: {
    title: tr('是否多选'),
    componentType: 'ButtonRadioGroup',
    // defaultValue: false,
    props: {
      dataSource: [
        {
          label: '单选',
          value: false,
        },
        {
          label: '多选',
          value: true,
        },
      ],
    },
  },
  precision: {
    title: tr('精度'),
    componentType: 'InputNumber',
    dependencies: ['integerOnly'],
    defaultValue: 0,
    onDependenciesChange: ([integerOnly], schema) => {
      return {
        ...schema,
        hide: !integerOnly,
      };
    },
    props: {
      min: 0,
      precision: 0,
    },
  },
  integerOnly: {
    title: tr('是否整数'),
    componentType: 'Switch',
    options: {
      valuePropName: 'checked',
    },
  },
  isUnitField: {
    title: tr('是否带单位'),
    componentType: 'Switch',
    options: {
      valuePropName: 'checked',
    },
  },
  unit: {
    title: tr('单位'),
    required: true,
    componentType: 'UnitSelector',
    dependencies: ['isUnitField'],
    onDependenciesChange: ([isUnitField], sechema) => {
      if (isUnitField === undefined || isUnitField === false) return { ...sechema, hide: true };
      return { ...sechema, hide: false };
    },
  },
  unitMoeny: {
    title: tr('金额单位'),
    aliasName: 'unit',
    required: true,
    componentType: 'UnitSelector',
    props: {
      defaultParams: {
        domain: 'money',
      },
      title: tr('金额查询'),
    },
  },
  decimalPrecision: {
    title: tr('小数位数'),
    componentType: 'InputNumber',
    props: {
      min: 0,
      precision: 0,
    },
  },
  maxCount: {
    title: tr('上传数量'),
    reactPropsName: 'maxLength',
    componentType: 'InputNumber',
    props: {
      min: 1,
      precision: 0,
    },
  },
  codeType: {
    title: tr('编码类型'),
    reactPropsName: 'type',
    required: true,
    componentType: 'CodeListSelector',
  },
};

let FieldPropsMapsT = { requiredFieldProps, defaultFieldProps, metadataFieldPropsMaps };

export function setFieldPropsMaps(maps: FieldPropsMaps) {
  FieldPropsMapsT = {
    requiredFieldProps: assign(FieldPropsMapsT.requiredFieldProps, maps.requiredFieldProps),
    defaultFieldProps: assign(FieldPropsMapsT.defaultFieldProps, maps.defaultFieldProps),
    metadataFieldPropsMaps: assign(FieldPropsMapsT.metadataFieldPropsMaps, maps.metadataFieldPropsMaps),
  };
  return FieldPropsMapsT;
}
export function getFieldPropsMaps() {
  return cloneDeep(FieldPropsMapsT);
}
